package com.hw.core.system.controller;

import com.hw.core.system.entity.Group;
import com.hw.core.system.service.GroupService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("group")
public class GroupController {
    @Autowired
    private GroupService groupService;

    @GetMapping("list")
    @RequiresPermissions(value = "分组查询")
    public String list(Model model) {
        model.addAttribute( "groups", groupService.findAll() );
        return "group/listGroup";
    }

    @PostMapping("add")
    @RequiresPermissions(value = "分组新增")
    @ResponseBody
    public String add(Group group) {
        if (groupService.add( group ) != null) {
            return "添加成功";
        }
        return "添加失败";
    }

    @GetMapping("add")
    @RequiresPermissions(value = "分组新增")
    public String add() {
        return "group/addGroup";
    }

    @GetMapping("edit")
    @RequiresPermissions(value = "分组修改")
    public String edit(Integer id, Model model) {
        model.addAttribute( "group", groupService.getGroupById( id ) );
        return "group/editGroup";

    }

    @PostMapping("edit")
    @RequiresPermissions(value = "分组修改")
    @ResponseBody
    public String edit(Group group) {
        groupService.update( group );
        return "修改成功";
    }

    @PostMapping("delete")
    @RequiresPermissions(value = "分组删除")
    @ResponseBody
    public String delete(Integer id) {
        groupService.delete( id );
        return "删除成功";
    }


}
